package net.huashitong.scheduling.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.utils.NumberUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.scheduling.entity.BranchCompany;
import net.huashitong.scheduling.filter.BranchCompanyFilter;
import net.huashitong.scheduling.service.BranchCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.scheduling.entity.BranchRanking;

import net.huashitong.scheduling.service.BranchRankingService;
@Controller
@RequestMapping("/app/branchRanking/")
public class BranchRankingController extends JsonBaseController{
	@Override
	 protected String getView(String arg0) {
			// TODO Auto-generated method stub
			return null;
	} 
	@Autowired
	private BranchRankingService branchRankingService;
	@Autowired
	private BranchCompanyService branchCompanyService;
	public static final String[] months = {"12","11","10","09","08","07","06","05","04","03","02","01"};
	public static final String[] years={"2022", "2021","2020","2019","2018","2017"};
	@ResponseBody
	@RequestMapping("/queryBranchRankingList")
	public Message queryBranchRankingList(@RequestBody IndicatorBreakdownFilter filter){
		 List<BranchRanking> bkList=new ArrayList<BranchRanking>();
		 Map<String, Object> maps = new HashMap<String, Object>();
		 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		 String time="";
		 int ss=0;
		 if(ObjectUtils.isBlank(filter.getDate())){
			 
			 for(int j=0;j<BranchRankingController.years.length;j++){
				 if(ss==1){
					 break;
				 }
			  	   for(int i=0; i<BranchRankingController.months.length;i++){
			  		 bkList=branchRankingService.queryByDate(years[j]+months[i]);
			  		if(ObjectUtils.isNotBlank(bkList)){
			  			time=years[j]+"年"+months[i]+"月";
			  			ss=1;
			  			break;
			  		}	
			  	 }
			 }
		 }else{
			 String month=filter.getDate().substring(4,6);
	  		 String year=filter.getDate().substring(0,4);
	  		 bkList=branchRankingService.queryByDate(year+month);
	  		 time=year+"年"+month+"月";
		 }
		 if(ObjectUtils.isNotBlank(bkList)){
			 int i=1;
			 for(BranchRanking br:bkList){
				 Map<String, Object> map = new HashMap<String, Object>();
				 map.put("name", br.getName());
				 map.put("number", br.getNumber());
				 map.put("currentMonth",br.getCurrentMonth());
				// map.put("currentCumulativeValue", NumberUtils.keepPrecision( br.getCurrentCumulativeValue(),2));//当年累计值
				// map.put("lastCumulativeValue",NumberUtils.keepPrecision( br.getLastCumulativeValue(),2) );//上年累计值
				// map.put("currentRatio",NumberUtils.keepPrecision( br.getCurrentRatio(),2));//当月同比
				 if(StringUtils.isBlank(br.getCumulativeRatio())){
					 map.put("cumulativeRatio","-");//累计同比
				 }else{
					 map.put("cumulativeRatio",NumberUtils.keepPrecision( br.getCumulativeRatio(),2));//累计同比
				 }
				 map.put("unit",br.getUnit());//单位
				 map.put("ranking", i);//排名
				 i++;
				 list.add(map);
			 }
		 }
//		 maps.put("title", "各单位挂钩联系限上商贸企业");
		 maps.put("title", "百人挂百企");
		 maps.put("lists", list);
		 maps.put("date", time);
		 return coverMessage("200","数据获取成功",maps);
	}
	@ResponseBody
	@RequestMapping("/queryBranchCompanyList")
	public Message queryBranchCompanyList(@RequestBody BranchCompanyFilter filter){
		List<BranchCompany> list=branchCompanyService.queryBranchCompanyList(filter);
		return coverMessage("200","数据获取成功",list);
	}
}
